<?php
//购物车
class Controller_WWW_Cart extends Controller_WWW_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:www/order/cart/detail";
        parent::__construct($req, $res);
    }
	
	//显示购物车
    public function action_index ()
    {
        $detailList = Cart::detailList($this->_user);
        if (empty($detailList))
            $this->template->set_filename('smarty:www/order/cart/empty');
        else
            $this->template->set('detailList', $detailList);
    }
    
	//删除购物车
	//$nid int 购物车id
    public function action_del ($nid)
    {
        Cart::remove($this->_user, array($nid));
        $this->request->redirect('/cart/index');
    }

	//添加到购物车
    public function action_add ()
    {
        $query    = $this->request->query();
        $type     = Arr::get($query, 'type');
        $goodsId  = Arr::get($query, 'goods');
        $attrId   = Arr::get($query, 'attr');
        $counts   = Arr::get($query, 'count');
        $startDay = Arr::get($query, 'startDay');
        $stopDay  = Arr::get($query, 'stopDay');

        $ret = Cart::add($this->_uid, $type, $goodsId, $attrId, $counts, $startDay, $stopDay);

        if ($this->request->is_ajax())
        {
            echo json_encode(array(
                'status' => 0,
                'message' => '',
            ));
            exit;
        }

        $this->request->redirect('/cart');
    }

	public function before ()
	{
		parent::before();
		if ($this->_uid == 0)
		{
            if ($this->request->is_ajax())
            {
                echo json_encode(array(
                    'status' => 2,
                    'message' => '未登录',
                ));
                exit;
            }
            else
            {
                $this->request->redirect('/login');
            }
		}
	}

}
